<template>
  <div class="app-container">
    <SearchContent :queryParams="queryParams" @query="handleQuery" @reset="resetQuery">
      <el-col :span="6">
        <el-form-item label="险种:" prop="insuranceItemId">
          <el-select v-model="queryParams.insuranceItemId" clearable style="width: 100%;">
            <template v-for="dict in dict.type.pro_duct_in_sur_ance">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车辆使用性质:" prop="vehicleUse">
          <el-select v-model="queryParams.vehicleUse" clearable style="width: 100%;">
            <template v-for="dict in dict.type.vehicle_use">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="保费标准地区:" prop="cityId">
          <el-select v-model="queryParams.cityId" clearable style="width: 100%;">
            <template v-for="dict in dict.type.pre_mi_um_area">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="座位数:">
          <el-input v-model="queryParams.seat" @input="queryParams.seat=queryParams.seat.replace(/^0|[^0-9]/g, '')"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="排量(L):" prop="liter">
          <el-input v-model="queryParams.liter" clearable @input="queryParams.liter=queryParams.liter.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="新车购置价:">
          <el-input v-model="queryParams.price" @input="queryParams.price=queryParams.price.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"></el-input>
        </el-form-item>
      </el-col>
    </SearchContent>

    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end" v-hasPermi="['synthesis:insurance:add']">
        <el-button type="primary" size="mini" @click="handleAdd">新增</el-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" border :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @setSizeCurrent="getList">
        <!-- 操作插槽 -->
        <template slot="tableColAc">
          <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <e-button size="mini" v-hasPermi="['synthesis:insurance:edit']" type="text" @click="handleUpdate(scope.row)">编辑
              </e-button>
              <el-button type="text" @click="voids(scope.row)" v-hasPermi="['synthesis:insurance:void']">作废</el-button>
            </template>
          </el-table-column>
        </template>
        <!-- 普通列插槽 -->
        <template slot="tableCol">
          <el-table-column label="险种" align="center" prop="insuranceItemName" />
          <el-table-column label="车辆使用性质" align="center" prop="vehicleUseName" />
          <el-table-column label="保费标准地区" align="center" prop="cityName" />
          <el-table-column label="最小座位数(含)" align="center" prop="startSeatCount" width="120" />
          <el-table-column label="最大座位数(含)" align="center" prop="endSeatCount" width="120" />
          <el-table-column label="最小排量(L)" align="center" prop="startLiter" />
          <el-table-column label="最大排量(L)(含)" align="center" prop="endLiter" width="140" />
          <el-table-column label="最小新车购置价(万元)" align="center" prop="startPrice" width="180">
            <template slot-scope="scope">
              <span>{{scope.row.startPrice}}</span>
            </template>
          </el-table-column>
          <el-table-column label="最大新车购置价(万元)(含)" align="center" prop="endPrice" width="220">
            <template slot-scope="scope">
              <span>{{scope.row.endPrice}}</span>
            </template>
          </el-table-column>
          <el-table-column label="保额(元)" align="center" prop="coverageAmount">
            <template slot-scope="scope">
              <span>{{scope.row.coverageAmount}}</span>
            </template>
          </el-table-column>
          <el-table-column label="保费(元)" align="center" prop="premium">
            <template slot-scope="scope">
              <span>{{scope.row.premium}}</span>
            </template>
          </el-table-column>
          <el-table-column label="费率(%)" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.rate">{{ numberRide(scope.row.rate,100)}}%</span>
              <span v-else>-</span>
            </template>
          </el-table-column>

        </template>
      </table-page>
    </div>

    <!-- 添加或修改测试单表对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body class="form-dialog" width="40%">
      <div style="width: 70%; margin: 0 auto;">
        <el-form ref="form" :model="form" label-width="180px" :rules="rules">
          <el-form-item label="险种:" prop="insuranceItemId">
            <el-select :disabled="disabled" v-model="form.insuranceItemId" clearable style="width: 100%;" @change="changeTag">
              <el-option v-for="dict in dict.type.pro_duct_in_sur_ance" :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="车辆使用性质:" prop="vehicleUse" v-if="form.insuranceItemId == '1' ||form.insuranceItemId == '2'||form.insuranceItemId == '3'||form.insuranceItemId == '4'||form.insuranceItemId == '5'|| form.insuranceItemId == '7'">
            <el-select v-model="form.vehicleUse" clearable style="width: 100%;" :disabled="disabled">
              <el-option v-for="dict in dict.type.vehicle_use" :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="保费标准地区:" prop="cityId" v-if="form.insuranceItemId == '1'||form.insuranceItemId == '2'||form.insuranceItemId == '3'||form.insuranceItemId == '4'|| form.insuranceItemId == '8'">
            <el-select v-model="form.cityId" clearable style="width: 100%;" :disabled="disabled">
              <el-option v-for="dict in dict.type.pre_mi_um_area" :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="最小座位数(含):" v-if="form.insuranceItemId == '1'||form.insuranceItemId == '2'||form.insuranceItemId == '3'||form.insuranceItemId == '4'|| form.insuranceItemId == '7'">
            <el-input :disabled="disabled" v-model="form.startSeatCount" maxlength="4" @input="form.startSeatCount=form.startSeatCount.replace(/^0|[^0-9]/g, '')" style="width: 100%;"></el-input>
          </el-form-item>
          <el-form-item label="最大座位数(含):" v-if="form.insuranceItemId == '1'||form.insuranceItemId == '2'||form.insuranceItemId == '3'||form.insuranceItemId == '4'|| form.insuranceItemId == '7'">
            <el-input :disabled="disabled" v-model="form.endSeatCount" maxlength="4" @input="form.endSeatCount=form.endSeatCount.replace(/^0|[^0-9]/g, '')" style="width: 100%;"></el-input>
          </el-form-item>
          <el-form-item label="最小排量(L):" prop="startLiter" v-if="form.insuranceItemId =='8'">
            <el-input :disabled="disabled" v-model="form.startLiter" maxlength="6" clearable @input="form.startLiter=form.startLiter.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"></el-input>
          </el-form-item>
          <el-form-item label="最大排量(L)(含):" prop="endLiter" v-if="form.insuranceItemId =='8'">
            <el-input :disabled="disabled" v-model="form.endLiter" maxlength="6" clearable @input="form.endLiter=form.endLiter.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"></el-input>
          </el-form-item>
          <el-form-item label="最小新车购置价(万元):" v-if="form.insuranceItemId == '6'">
            <el-input :disabled="disabled" maxlength="8" v-model="form.startPrice" @input="form.startPrice=form.startPrice.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')" style="width: 100%;"></el-input>
          </el-form-item>
          <el-form-item label="最大新车购置价(万元):" v-if="form.insuranceItemId == '6'">
            <el-input :disabled="disabled" maxlength="8" v-model="form.endPrice" @input="form.endPrice=form.endPrice.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')" style="width: 100%;"></el-input>
          </el-form-item>
          <el-form-item label="保额(元):" prop="coverageAmount" v-if="['1','5','6'].includes(form.insuranceItemId)">
            <el-input v-model="form.coverageAmount" maxlength="13" clearable @input="form.coverageAmount=form.coverageAmount.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"></el-input>
          </el-form-item>
          <el-form-item label="保费(元):" prop="premium" v-if="['1','4','5','6','7','8'].includes(form.insuranceItemId)">
            <el-input v-model="form.premium" maxlength="13" clearable @input="form.premium=form.premium.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"></el-input>
          </el-form-item>
          <el-form-item label="费率(%):" prop="rate" v-if="['2','3','4'].includes(form.insuranceItemId)">
            <el-input v-model="form.rate" maxlength="13" clearable @input="form.rate=form.rate.replace(/^\D*([0-9]\d*\.?\d{0,5})?.*$/,'$1')"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="cancel">取消</el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import SearchContent from "@/components/SearchContent/index.vue";
import {
  getFeeBaseList,
  feeBaseAdd,
  feeBaseUpdate,
  feeBaseDelete,
  feeBaseDetail,
} from "@/api/globalField";

export default {
  name: 'InsuranceIndex',

  components: {
    SearchContent,
  },
  dicts: ["vehicle_use", "pro_duct_in_sur_ance", "pre_mi_um_area"],
  data() {
    return {
      rules: {
        insuranceItemId: [
          { required: true, message: "请选择险种", trigger: "blur" },
        ],
        vehicleUse: [
          { required: true, message: "请选择车辆使用性质", trigger: "blur" },
        ],
        cityId: [
          { required: true, message: "请选择保费标准地区", trigger: "blur" },
        ],
      },
      disabled: false,
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      isNew: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dataList: [],
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeTag() {
      this.form.coverageAmount = "";
      this.form.premium = "";
      this.form.rate = "";
    },
    voids(row) {
      this.$confirm("确认作废?", "作废", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          feeBaseDelete(row.id).then((res) => {
            this.$message({
              type: "success",
              message: "作废成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消作废",
          });
        });
    },
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      getFeeBaseList(this.queryParams).then((res) => {
        if (res.code == "200") {
          const { rows, total } = res;
          this.dataList = rows;
          this.total = total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    reset() {
      this.form = {
        includeStartSeatCount: 1,
        includeEndSeatCount: 1,
        includeStartLiter: 0,
        includeEndLiter: 1,
        includeStartPrice: 0,
        includeEndPrice: 1,
        insuranceItemId: "1",
        rate: "",
        premium: "",
        coverageAmount: "",
        startLiter: '',
        endLiter: ''
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      }),
        this.handleQuery();
    },
    /** 新增操作 */
    handleAdd() {
      this.open = true;
      this.title = "新增";
      this.disabled = false;
      this.$refs["form"] && this.$refs["form"].resetFields();
      this.reset();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.open = true;
      this.title = "编辑";
      this.disabled = true;
      feeBaseDetail(row.id).then((res) => {
        this.form = res.data;
        this.form.rate = this.numberRide(this.form.rate, 100)
      });
    },
    saveData() {
      let form = this._.cloneDeep(this.form)
      form.rate = this.numberExcept(this.form.rate, 100)
      if (this.title == "新增") {
        feeBaseAdd(form).then((res) => {
          this.cancel();
          this.getList();
          this.$message.success("新增成功");
        });
      } else {
        // 调取接口
        feeBaseUpdate(form).then((res) => {
          this.cancel();
          this.getList();
          this.$message.success("编辑成功");
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.insuranceItemId == '1' || this.form.insuranceItemId == '2' || this.form.insuranceItemId == '3' || this.form.insuranceItemId == '4' || this.form.insuranceItemId == '7') {
            if (
              Number(this.form.endSeatCount) >= Number(this.form.startSeatCount)
            ) {
              this.saveData()
            } else {
              this.$message.error("最大座位数必须大于或等于最小座位数,请重新输入");
              this.form.endSeatCount = null;
              return false;
            }
          } else if (this.form.insuranceItemId == '6') {
            if (
              Number(this.form.endPrice) >= Number(this.form.startPrice)
            ) {
              this.saveData()
            } else {
              this.$message.error("最大新车购置价必须大于或等于最小新车购置价,请重新输入");
              this.form.endPrice = null;
              return false;
            }
          } else if (this.form.insuranceItemId == '8') {
            if (
              Number(this.form.endLiter) >= Number(this.form.startLiter)
            ) {
              this.saveData()
            } else {
              this.$message.error("最大排量必须大于或等于最小排量,请重新输入");
              this.form.endLiter = null;
              return false;
            }
          } else {
            this.saveData()
          }
        }
      });
    },
  },
};
</script>


  <style lang="scss" scoped>
.pagination-container {
  padding: 0 20px 50px !important;
}

.form-dialog {
  ::v-deep .el-dialog__header {
    padding: 10px 20px;
    border-bottom: 1px solid #dcdfe6;

    .el-dialog__headerbtn {
      top: 15px;
    }
  }

  .box-card {
    margin-bottom: 20px;

    .card-title {
      font-size: 16px;
      position: relative;
      margin-left: 8px;
      padding: 0;
    }

    .card-title::after {
      content: "";
      width: 4px;
      height: 18px;
      background-color: aqua;
      display: block;
      position: absolute;
      left: -12px;
      top: 2px;
    }
  }
}
</style>
